package com.quantgroup.data.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.google.common.base.Stopwatch;
import com.quantgroup.data.config.FirstApolloConfig;
import com.quantgroup.data.metrics.DefaultCollector;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;


/**
 * Test
 *
 * @author osmondy
 * @create 2021/10/18 20:05
 */
@RestController
public class IndexController {

    @Resource
    FirstApolloConfig firstApolloConfig;

    @SentinelResource(value = "/resource", blockHandler = "haderErrA")
    @GetMapping("/")
    public String test() {
        return "Current env is " + firstApolloConfig.getEnv();
    }

    public String haderErrA(BlockException b) {
        return "1234";
    }


//    @GetMapping("/{channel}/123")
//    public String test2(@PathVariable("channel") String channel) {
//        return "Current env is " + sampleProp.getEnv();
//    }



    @GetMapping("/test222")
    public String test222() {
//        metricsCollector.requestMetrics("ssss");

//        registry.timer("api_cost_timer").record(1000, TimeUnit.MILLISECONDS);
//        requestCounter.labels("aa", "bb").inc();


        Stopwatch stopwatch = Stopwatch.createStarted();

        Stopwatch stop = stopwatch.stop();
        long time = stop.elapsed(TimeUnit.MILLISECONDS);

        DefaultCollector.requestSpendMetrics("/test222", time);
        DefaultCollector.requestMetrics("/test222");


//        histogram.labels("aaaa", "bbb", "cccc");
        return "test";
    }


}
